package utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ListSplitUtils {

    public static <T> List<List<T>> split(List<T> list, int block) {
        if (null == list) {
            return Collections.singletonList(null);
        }
        if (list.size() <= block) {
            return Collections.singletonList(list);
        }

        int count = list.size() / block;
        if (list.size() % block > 0) {
            count++;
        }

        List<List<T>> result = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int start = i * block;
            int end = i == count - 1 ? list.size() : start + block;
            result.add(list.subList(start, end));
        }
        return result;
    }

    public static void main(String[] args) {
        List<List<Integer>> result = split(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 4);
        System.out.println(result);
    }

}
